/*
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

package org.geohunter.activity.osmmap;

import org.andnav.osm.ResourceProxy;
import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
import org.geohunter.Geocache;
import org.geohunter.GeocacheList;
import org.geohunter.GraphicsGenerator;
import org.geohunter.activity.main.GuiState;
import org.geohunter.activity.map.CacheHint;
import org.geohunter.activity.map.ICachePinsOverlayFactory;
import org.geohunter.activity.map.OverlayManager;
import org.geohunter.database.DbFrontend;

import android.graphics.drawable.Drawable;
import android.util.Log;

import java.util.ArrayList;

public class OsmCachePinsOverlayFactory 
implements ICachePinsOverlayFactory<OpenStreetMapViewOverlay> {
    private final GraphicsGenerator mGraphicsGenerator;
    private final DbFrontend mDbFrontend;
    private final CacheHint mCacheHint;
    private final GuiState mGuiState;
    private final ResourceProxy mResourceProxy;
    
    public OsmCachePinsOverlayFactory(GraphicsGenerator graphicsGenerator,
            DbFrontend dbFrontend,
            CacheHint cacheHint,
            GuiState guiState,
            ResourceProxy resourceProxy) {
        mGraphicsGenerator = graphicsGenerator;
        mDbFrontend = dbFrontend;
        mCacheHint = cacheHint;
        mGuiState = guiState;
        mResourceProxy = resourceProxy;
    }
    
    private OsmPinItem createCacheItem(Geocache geocache, boolean selected) {
        Drawable icon;
        if (selected) {
            icon = mGraphicsGenerator.getMapIcon(geocache, true, mDbFrontend);
        } else {
            icon = geocache.getIconMap();
            if (icon == null) {
                icon = mGraphicsGenerator.getMapIcon(geocache, false, mDbFrontend);
                geocache.setIconMap(icon);
            }
        }
        OsmPinItem item = new OsmPinItem(geocache, icon);
        return item;
    }

    @Override
    public OsmPinOverlay makeOverlay(Geocache selected, GeocacheList list,
            OverlayManager<OpenStreetMapViewOverlay> manager) {
        final ArrayList<OsmPinItem> items = 
            new ArrayList<OsmPinItem>(list.size());
        Log.d("geohunter", "makeOverlay for " + list.size() + " items");
        for (Geocache geocache : list) {
            boolean isSelected = (geocache == selected);
            items.add(createCacheItem(geocache, isSelected));
        }

        OsmPinOverlay.OnItemTapListener<OsmPinItem> onTap =
            new OnPinTapListener(mGuiState, manager, mCacheHint, items);
        
        return new OsmPinOverlay(items, onTap, mResourceProxy);
    }
}